﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.DanhMuc;
using Webdiyer.WebControls.Mvc;
using DTTB.Common;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.DanhMuc;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.DanhMucDelegate
{
    public class BuoiDelegate
    {
        DBDanhMucDataContext DBDanhMuc = new DBDanhMucDataContext();

        public IList<dm_Buoi> GetAllBuoi()
        {
            return DBDanhMuc.dm_Buois.Where(p => p.IsDelete == false).ToList();
        }
        public dm_Buoi AddNewBuoi(dm_Buoi Buoi)
        {
            DBDanhMuc.dm_Buois.InsertOnSubmit(Buoi);

            DBDanhMuc.SubmitChanges();

            return GetLoaiCongChucIdMax();
        }

        public void DeleteBuoi(int BuoiID)
        {
            dm_Buoi BuoiNew =
                DBDanhMuc.dm_Buois.Single(p => p.Id == BuoiID);
            //LoaiCongChuc.Id = LoaiCongChuc.Id;

            DBDanhMuc.dm_Buois.DeleteOnSubmit(BuoiNew);
            DBDanhMuc.SubmitChanges();
        }
        public int DeleteBuoi(dm_Buoi Buoi)
        {
            try
            {
                dm_Buoi BuoiDelete = DBDanhMuc.dm_Buois.Single(p => p.Id == Buoi.Id);
                BuoiDelete.Id = Buoi.Id;
                BuoiDelete.UpdateDate = DateTime.Now;
                BuoiDelete.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
                BuoiDelete.IsDelete = true;
                DBDanhMuc.SubmitChanges();
                return 1;
            }
            catch(Exception)
            {return 0;}
            
        }
        public void UpdateBuoi(dm_Buoi Buoi)
        {
            dm_Buoi BuoiUpdate =
                DBDanhMuc.dm_Buois.Single(p => p.Id == Buoi.Id);

            BuoiUpdate.Id = Buoi.Id;
            BuoiUpdate.Name = Buoi.Name;
            BuoiUpdate.UpdateDate = BuoiUpdate.UpdateDate;
            BuoiUpdate.UpdateBy = BuoiUpdate.UpdateBy;
            DBDanhMuc.SubmitChanges();
        }

        public PagedList<dm_Buoi> SearchBuoiPagedList(int? id, string searchCriteria)// id = page Index
        {
            return (PagedList<dm_Buoi>)DBDanhMuc.dm_Buois.Where(p => p.Name.Contains(searchCriteria) && p.IsDelete == false).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);


        }

        public dm_Buoi GetBuoiById(string id)
        {
            try
            {
                dm_Buoi item = this.DBDanhMuc.dm_Buois.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }
        public PagedList<dm_Buoi> RecycleGetBuoiPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_Buois.Where(p => p.IsDelete == true).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }
        public PagedList<dm_Buoi> GetBuoiPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_Buois.Where(p => p.IsDelete == false).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }

        public dm_Buoi BuildNewBuoi(BuoiForm form)
        {
            dm_Buoi newBuoi = new dm_Buoi();
            newBuoi.Name = form.Name;
            newBuoi.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newBuoi.CreateDate = DateTime.Now;
            newBuoi.IsDelete = false;
            return newBuoi;
        }
        
        public dm_Buoi BuildEditBuoi(BuoiForm form)
        {
            dm_Buoi newBuoi = new dm_Buoi();
            newBuoi.Id = form.Id;
            newBuoi.Name = form.Name;
            newBuoi.UpdateDate = DateTime.Now;
            newBuoi.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            return newBuoi;
        }
        public dm_Buoi GetLoaiCongChucIdMax()
        {
            IList<dm_Buoi> list = DBDanhMuc.dm_Buois.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }
        public BuoiForm ConvertToForm(dm_Buoi obj)
        {
            BuoiForm form = new BuoiForm();
            form.Id = obj.Id;
            form.Name = obj.Name;
            form.UpdateDate = obj.UpdateDate != null ? obj.UpdateDate.Value : DateTime.MinValue;
            form.CreateDate = obj.CreateDate != null ? obj.CreateDate.Value : DateTime.MinValue;
            form.UpdateBy = obj.UpdateBy != null ? obj.UpdateBy.Value : 0;
            form.CreateBy = obj.CreateBy != null ? obj.CreateBy.Value : 0;
            return form;
        }
    }
}